using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;

namespace TCEntity
{
  public class Image
  {
    public Image()
    {
      this.IsShared = false;
      this.IsLocal = true;
      this.ImageCategory = new byte[5];
    }
    public Image(int id, string imagePath)
    {
      this.Id = id;
      this.ImagePath = imagePath;
      this.IsShared = false;
      this.IsLocal = true;
      this.ImageCategory = new byte[5];
    }
    public int Id { get; set; }

    public string Username { get; set; }
    public DateTime UploadTime { get; set; }
    public string ImagePath { get; set; }
    public byte[] Picture { get; set; }

    public byte[] ImageCategory { get; set; }//目[0]代表人物，[1]代表风景,[2]代表动物，[3]代表汽车，
                                             //[4]代表动漫。
    public bool IsShared { get; set; }
    public bool IsLocal { get; set; }
    public static byte[] Getbyte(string imageUrl)
    {

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
      request.Proxy = null;
      request.Accept = "*/*";
      request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
      request.Referer = "";
      request.Timeout = 30000;
      request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
      request.Method = "GET";
      request.KeepAlive = false;
      request.ProtocolVersion = HttpVersion.Version10;
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      if (response.StatusCode != HttpStatusCode.OK)
        return null;
      Stream resStream = response.GetResponseStream();
      Bitmap bmp = new Bitmap(resStream);
      response.Close();
      request.Abort();

      using (MemoryStream curImageStream = new MemoryStream())
      {
        bmp.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
        curImageStream.Flush();
        byte[] bmpBytes = curImageStream.ToArray();
        return bmpBytes;
      }
    }

  }
}
